import { Component, OnInit, ViewChild, Injector, AfterViewInit, ViewEncapsulation } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { SelectItem } from 'primeng/api';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';
import { WorkingHourServiceProxy, LibraryOfLaborServiceProxy, EmployeeSalaryDto, ProductionOutput, ReportsForMonthServiceProxy, ReportsForLaborHourMonthOutput } from '@shared/service-proxies/service-proxies';
//import { ExcelModalComponent } from '@app/shared/common/excel-modal/excel-modal.component';
import * as moment from 'moment';
import { BsDatepickerViewMode } from 'ngx-bootstrap/datepicker/models';
import { BsDatepickerConfig, BsDaterangepickerConfig } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import * as G2 from '@antv/g2';
import { PrimengTableHelper } from '@shared/helpers/PrimengTableHelper';
import * as FileSaver from 'file-saver';
import * as XLSX from 'xlsx';
import { FileDownloadService } from '@shared/utils/file-download.service';

@Component({
  templateUrl: './report-month.component.html',
  styleUrls: ['./report-month.component.css'],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()]

})
export class ReportMonthComponent extends AppComponentBase implements OnInit {
  ngOnInit(): void {
    this.bsConfig = Object.assign({}, {
      minMode: this.minMode,
      dateInputFormat: 'MM-YYYY',
      returnFocusToInput: true
    });
    this.filterStartTime = null;
    this.filterEndTime = null;
  }


  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('paginatorNormal') paginatorNormal: Paginator;
  @ViewChild('dataTableNormalProductions') dataTableNormalProductions: Table;

  bsConfig: Partial<BsDatepickerConfig>;
  minMode: BsDatepickerViewMode = 'month';
  exporting: boolean = false;
  advancedFiltersAreShown: Boolean = false;


  primengTableHelperNormalReport = new PrimengTableHelper();
  paginatorReport = new Paginator();


  filterStartTime: Date;
  filterEndTime: Date;
  filterPicNo: string = '';
  filterName: string = '';
  filterModel: string = '';
  filterPart: string = '';
  filterDpt: string = '';
  filterMainTaskCode: string = '';
  filterTeamGroupName: string = '';
  filterTaskNo = '';


  workShopList = [
    { value: '51', displayText: '51' },
    { value: '53', displayText: '53' },
    { value: '55', displayText: '55' },
    { value: '56', displayText: '56' },
    { value: '57', displayText: '57' },
    { value: '58', displayText: '58' }
  ];

  constructor(
    injector: Injector,
    private _reportsForMonthServiceProxy: ReportsForMonthServiceProxy,
    private _fileDownloadService: FileDownloadService,
  ) {
    super(injector);

  }


  getReport(event?: LazyLoadEvent) {
    if (this.primengTableHelperNormalReport.shouldResetPaging(event)) {
      this.paginatorNormal.changePage(0);
      return;
    }

    this.primengTableHelperNormalReport.showLoadingIndicator();

    let startYear = 0;
    let startMonth = 0;
    let endYear = 0;
    let endMonth = 0;

    if (this.filterStartTime != null) {
      startYear = this.filterStartTime.getFullYear();
      startMonth = this.filterStartTime.getMonth() + 1;

    }

    if (this.filterEndTime != null) {
      endYear = this.filterEndTime.getFullYear();
      endMonth = this.filterEndTime.getMonth() + 1;
    }

    this._reportsForMonthServiceProxy.getAllReportsForMonth(
      startYear,
      startMonth,
      endYear,
      endMonth,
      this.filterDpt,
      this.filterModel,
      this.filterMainTaskCode,
      this.filterPart,
      this.filterName,
      this.filterPicNo,
      this.filterTeamGroupName,
      '',
      this.primengTableHelperNormalReport.getSorting(this.dataTableNormalProductions),
      this.primengTableHelperNormalReport.getMaxResultCount(this.paginatorNormal, event),
      this.primengTableHelperNormalReport.getSkipCount(this.paginatorNormal, event)
    ).pipe(finalize(() => this.primengTableHelperNormalReport.hideLoadingIndicator()))
      .subscribe((result) => {
        this.primengTableHelperNormalReport.totalRecordsCount = result.totalCount;
        this.primengTableHelperNormalReport.records = result.items;
        this.primengTableHelperNormalReport.hideLoadingIndicator();
      });
  }

  exportToExcel(): void {

    this.primengTableHelperNormalReport.showLoadingIndicator();
    let startYear = 0;
    let startMonth = 0;
    let endYear = 0;
    let endMonth = 0;

    if (this.filterStartTime == null) {
      this.primengTableHelperNormalReport.hideLoadingIndicator();
      this.message.warn('请选择月份，再进行导出！');
      return;
    }

    if (this.filterStartTime != null) {
      startYear = this.filterStartTime.getFullYear();
      startMonth = this.filterStartTime.getMonth() + 1;
    }

    if (this.filterEndTime != null) {
      endYear = this.filterEndTime.getFullYear();
      endMonth = this.filterEndTime.getMonth() + 1;
    }
    this._reportsForMonthServiceProxy.getUsersToExcel(
      startYear,
      startMonth,
      endYear,
      endMonth,
      '', '', '', '', '', '', '',
      '',
      '',
      1,
      1
    ).pipe(finalize(() => this.primengTableHelperNormalReport.hideLoadingIndicator()))
      .subscribe(result => {
        this._fileDownloadService.downloadTempFile(result);

        this.primengTableHelperNormalReport.hideLoadingIndicator();
      });
  }




  reloadPage(): void {
    this.paginatorNormal.changePage(this.paginatorNormal.getPage());

  }



}